<?php namespace App\Http\C5;

use p5\objects\TimeUtils;
use Illuminate\Support\Facades\Log;

/**
 * Created by PhpStorm.
 * User: simon
 * Date: 2016/3/8
 * Time: 11:18
 */
class C5ResponseFactory
{

    static public function newSuccessInstance($result = "", $code = "ok", $msg = "")
    {
        $code = empty($code) ? "ok" : $code;
        $bean = new C5Response();
        $bean->setCode($code);
        $bean->setMessage($msg);
        $bean->setResult($result);
        return $bean;
    }

    static public function newFailedInstance($code = "", $message = "", $result = "")
    {
        $bean = new C5Response();
        $bean->setSuccess(false);
        $bean->setCode($code);
        $bean->setMessage($message);
        $bean->setResult($result);
        return $bean;
    }

    /**
     * @param $exception \Exception
     * @param string $code
     * @return C5Response
     */
    static public function newExceptionInstance($exception, $code = "")
    {
        Log::error($exception->getCode());
        Log::error($exception->getMessage());
        Log::error($exception->getTraceAsString());
        $code = empty($code) ? $exception->getCode() : $code;
        $bean = new C5Response();
        $bean->setSuccess(false);
        $bean->setCode($code);
        $bean->setMessage($exception->getMessage());
        return $bean;
    }

    static public function newRecordNotExistsInstance()
    {
        $bean = new C5Response();
        $bean->setCode("record.not.exists");
        $bean->setMessage("出错了,记录不存在");
        $bean->setResult(0);
        return $bean;
    }

    static public function newRecordUsedInstance($msg="数据已经被使用,存在关联数据.不能被删除.")
    {
        $bean = new C5Response();
        $bean->setSuccess(false);
        $bean->setCode("record.used");
        $bean->setMessage($msg);
        $bean->setResult(0);
        return $bean;
    }

}